package cn.dyp.com.netconnection.activity;

import java.util.ArrayList;
import java.util.List;



import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import cn.dyp.com.netconnection.R;
import cn.dyp.com.netconnection.adapter.DataAdapter;
import cn.dyp.com.netconnection.bean.SensorDataBean;
import cn.dyp.com.netconnection.util.NetUtil;

/**
 * App的主控制Activity类
 * 
 * @author asus
 */

/*
看到这个模块的时候突然发现之前实现app全屏的方式不对，仅仅是隐藏了标题栏，没有隐藏信息栏，所以添上：
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

 */

public class MainActivity extends AppCompatActivity {
	private static final int IP = 0x00;
	private static final int NET = 0x01;
	private static final int UP_DATE = 0x02;
	private static final int SHOW_DIALOG = 0x03;
	private EditText mSetIpET;
	private Button mGetDataBtn;
	private ListView mDataLV;
	private DataAdapter mAdapter;
	private List<SensorDataBean> mList;
	private Thread mThread;
	private String mResultString;
	private ProgressDialog mDialog;
	private Handler mHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//隐藏标题栏
		getSupportActionBar().hide();
		//隐藏信息栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);
		getDialog();
		handleMessage();
		iniData();
		iniView();
		setListener();
		setListView();
	}

	/**
	 * 消息处理
	 * 
	 */
	private void handleMessage() {
		mHandler = new Handler(new Handler.Callback() {
			public boolean handleMessage(Message msg) {
				mDialog.dismiss();
				switch (msg.what) {
				case IP:
					Toast.makeText(MainActivity.this, R.string.ip_msg,
							Toast.LENGTH_SHORT).show();
					break;
				case NET:
					Toast.makeText(MainActivity.this, R.string.net_msg,
							Toast.LENGTH_SHORT).show();
				case UP_DATE:
					mAdapter.notifyDataSetChanged();
					break;
				case SHOW_DIALOG:
					mDialog.show();
					break;
				}
				return false;
			}
		});
	}

	/**
	 * 获取WaitingDialog
	 * 
	 */
	private void getDialog() {
		mDialog = new ProgressDialog(this);
		mDialog.setMessage(getText(R.string.get_data_msg));
	}

	/**
	 * 初始化数据
	 */
	private void iniData() {
		mList = new ArrayList<SensorDataBean>();
	}

	/**
	 * 设置ListView
	 */
	private void setListView() {
		mAdapter = new DataAdapter(MainActivity.this, mList);
		mDataLV.setAdapter(mAdapter);
	}

	/**
	 * 启动网络连接线程
	 */
	private void setThread() {
		mThread = new Thread() {
			@Override
			public void run() {
				super.run();
				if (NetUtil.isNetworkAvailable(MainActivity.this)) {
					if (!getIp().equals("")) {
						mHandler.sendEmptyMessage(SHOW_DIALOG);
						try {
							mResultString = NetUtil
									.sendData(
											"http://"
													+ getIp()
													+ ":8890/type/jason/action/getSensor",
											"{}");
							parseJason();
						} catch (JSONException e) {
							e.printStackTrace();
						}
						mHandler.sendEmptyMessage(UP_DATE);
					} else {
						mHandler.sendEmptyMessage(IP);
					}
				} else {
					mHandler.sendEmptyMessage(NET);
				}
			}
		};
	}

	/**
	 * 设置监听器
	 */
	private void setListener() {
		mGetDataBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setThread();
				mThread.start();
			}
		});
	}

	/**
	 * 获取用户输入的Ip
	 * 
	 * @return Ip字符串
	 */
	private String getIp() {
		return mSetIpET.getText().toString().trim();
	}

	/**
	 * 初始化控件
	 */
	private void iniView() {
		mGetDataBtn = (Button) findViewById(R.id.get_data_btn);
		mSetIpET = (EditText) findViewById(R.id.ip_ed);
		mDataLV = (ListView) findViewById(R.id.data_show_listview);
	}

	/**
	 * 解析JSON数据
	 * 
	 * @throws JSONException
	 */
	private void parseJason() throws JSONException {
		JSONObject mjson = new JSONObject(mResultString);
		SensorDataBean mBean = new SensorDataBean();
		mBean.setCo2(mjson.getInt("co2"));
		mBean.setLight(mjson.getInt("light"));
		mBean.setAirT(mjson.getInt("airTemperature"));
		mBean.setAirH(mjson.getInt("airHumidity"));
		mBean.setSoilT(mjson.getInt("soilTemperature"));
		mBean.setSoilH(mjson.getInt("soilHumidity"));
		mList.add(mBean);
	}
}
